﻿using HSPCS.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HSPCS.Web
{
    public partial class About : Page
    {
        DataBLL dataBLL = new DataBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                Literal myLiteral = new Literal();
                myLiteral.Text = "<h4>" + tableName + "</h4>";
                myPlaceHolder.Controls.Add(myLiteral);
                ShowColumn(tableName);
            }
            else
            {
                Literal myLiteral = new Literal();
                myLiteral.Text = "<h4>No table was found.</h4>";
                myPlaceHolder.Controls.Add(myLiteral);
            }

        }
        public void ShowColumn(string tableName)
        {
            DataTable listColumn = new DataTable();
            listColumn = dataBLL.GetColumn(tableName);
            if (listColumn.Rows.Count > 0)
            {
                Literal myLiteralB = new Literal();
                myLiteralB.Text = "<table>";
                myPlaceHolder.Controls.Add(myLiteralB);

                foreach (DataRow r in listColumn.Rows)
                {
                    Literal myLiteral1 = new Literal();
                    myLiteral1.Text = "<tr> <td>";
                    myPlaceHolder.Controls.Add(myLiteral1);

                    Label myLabel = new Label();
                    myLabel.Text = r[0].ToString() + ": ";
                    myPlaceHolder.Controls.Add(myLabel);

                    Literal myLiteral2 = new Literal();
                    myLiteral2.Text = "</td><td>";
                    myPlaceHolder.Controls.Add(myLiteral2);

                    if (r[5].ToString() == "0")
                    {
                        TextBox myTextBox = new TextBox();
                        myTextBox.ID = "txt" + r[0].ToString();
                        myTextBox.Attributes.Add("class", "textbox");
                        myPlaceHolder.Controls.Add(myTextBox);
                    }
                    else
                    {
                        DropDownList myDropDownList = new DropDownList();
                        myDropDownList.ID = "ddl" + r[0].ToString();
                        DataTable foreignData = dataBLL.GetForeignKey(tableName, r[0].ToString());
                        if(foreignData!= null)
                        {
                            myDropDownList.DataTextField = foreignData.Columns[1].ToString();
                            myDropDownList.DataValueField = foreignData.Columns[0].ToString();
                            myDropDownList.DataSource = foreignData;
                            myDropDownList.DataBind();
                            myDropDownList.Attributes.Add("class", "dropdownlist");
                        }
                        myPlaceHolder.Controls.Add(myDropDownList);
                    }
                    Literal myLiteral3 = new Literal();
                    myLiteral3.Text = "</td></tr>";
                    myPlaceHolder.Controls.Add(myLiteral3);
                }

                Literal myLiteralE = new Literal();
                myLiteralE.Text = "</table>";
                myPlaceHolder.Controls.Add(myLiteralE);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (var control in myPlaceHolder.Controls)
            {
                if (control is TextBox)
                { 
                    
                }
            }
        }
    }
}